﻿/**
 *
 * @author xugy,cuijb
 *
 **/
function add(inputID, listID, recordID, namespace, helper) {
	if (inputID.isEmpty() || listID.isEmpty()) {
		return;
	}
	var templet = document.getElementById(inputID);
	var currentRow = document.getElementById(listID).insertRow();
	var array = tree.childs(templet, 1);
	currentRow.setAttribute("helper", helper);
	currentRow.setAttribute("namespace", namespace);
	currentRow.setAttribute("identity", recordID);
	currentRow.setAttribute("bgColor", "#F6F6F6");
	for (var i = 0; i < array.length; i++) {
		currentRow.insertCell();
		currentRow.cells[i].setAttribute("width", array[i].getAttribute("width"));
		currentRow.cells[i].setAttribute("align", "center");
		currentRow.cells[i].getAttribute("style").setAttribute("display", array[i].getAttribute("style").getAttribute("display"));
		//immutable
		if (array[i].getAttribute("immutable") == "1") {
			array[i] = tree.childs(array[i], 1)[0];
			currentRow.cells[i].setAttribute("readonly", 1);
			currentRow.cells[i].innerHTML = "<span id=fix_" + array[i].getAttribute("name") + ">" + array[i].getAttribute("value") + "</span>";
			continue;
		}
		//operator
		if (array[i].getAttribute("id") == "operator") {
			currentRow.cells[i].innerHTML = "<a href='javascript:void(0);' onclick='javascript:update(this);'>\u4fee\u6539</a></br></br><a href='javascript:void(0);' onclick='javascript:remove(this);'>\u5220\u9664</a>";
			continue;
		}
		currentRow.cells[i].innerHTML = array[i].innerHTML;
	}
}
function update(evt) {
	evt = evt ? evt : (window.event.srcElement ? window.event.srcElement : null);
	var row = evt.parentNode.parentNode;
	
	var TdObject = row.children;
	for (var i = 0; i < TdObject.length - 1; i++) {
		object = TdObject.item(i).children.item(0);
		if(object.name=='name'){
			var value=object.value;
			if(isNullOrEmpty(value)){
				alert('请填写名称！');
				return;
			}
		}
		if(object.name=='sequence'){
			
			if(isNullOrEmpty(object.value)){
			alert('请填写顺序！');
			return;
			}else{
			if(strIsNum(object.value)){
				
				if(object.value>127){
					alert('顺序值只能填写 0~127之间的数字！');
					return;
				}
				}else{
					alert('顺序只能填写 数字！');
					return;
				}
			}
		}
			/*
		if(object.name=='value'){
			value=object.value;
			if(!isNullOrEmpty(value)){
				alert(value);
				 var reg=/^[0-9]*@@@[[\u4e00-\u9fa5][0-9][a-z][A-Z]].*;$|\\s|$/;
				alert(reg.test(value));
				 if(!reg.test(value)){
				 	
				 }else{
				 	alert("请按照合法格式填写（事例：1@@@可用;2@@@不可用;）");
				 	return;
				 }
			}
		}
		*/
	}
	
	
	eval(row.helper).updateRecord(row.namespace + ".change", getJson(row), {callback:function (returnValue) {
		if (returnValue <= 0) {
			alert("\u66f4\u65b0\u5931\u8d25");
		}
	}});
}
function remove(evt) {
	evt = evt ? evt : (window.event.srcElement ? window.event.srcElement : null);
	var row = evt.parentNode.parentNode;
	var target = evt.parentNode.parentNode.parentNode;
	//alert(row.identity);
	eval(row.helper).removeRecord(row.namespace + ".remove", row.identity, {callback:function (returnValue) {
		if (returnValue > 0) {
			//alert(row.innerHTML);
			if(row.helper == "searchDetailHelper"){
				addOption(document.getElementById("keywords") ,(tree.item(row, "fix_keywords")).innerText);
			}
			target.deleteRow(row.rowIndex);
		} else {
			alert("\u5220\u9664\u5931\u8d25");
		}
	}});
}
function getJson(TrObject) {
	var object;
	var id = TrObject.identity;
	//var TrObject = document.getElementById(TrName);
	var json = "{";
	var TdObject = TrObject.children;
	for (var i = 0; i < TdObject.length - 1; i++) {
		if (TdObject.item(i).readonly == "1") {
			continue;
		}
		object = TdObject.item(i).children.item(0);
		if (i == 0) {
			json = json + object.name + ":\"" + object.value + "\"";
		} else {
			json = json + "," + object.name + ":\"" + object.value + "\"";
		}
		/*
		if (i == TdObject.length - 2) {
			json = json + object.name + ":"" + object.value + """;
		} else {
			json = json + object.name + ":"" + object.value + "",";
		}
		*/
	}
	if (!isNullOrEmpty(id)) {
		json = json + ",id:\"" + id + "\"";
	}
	json = json + "}";
	//alert(json);
	//alert(json);
	var Parameter = eval("(" + json + ")");
	return Parameter;
}

function addRecord(input, list, namespace, helper) {

	var TrObject=document.getElementById(input);
	var TdObject = TrObject.children;
	for (var i = 0; i < TdObject.length - 1; i++) {
		object = TdObject.item(i).children.item(0);
		
		if(object.name=='name'){
			var value=object.value;
			if(isNullOrEmpty(value)){
				alert('请填写名称！');
				return;
			}
		}
		/*
		if(object.name=='unit'){
			var value=object.value;
			if(isNullOrEmpty(value)){
				alert('请填写单位！');
				return;
			}
		}
		*/
		if(object.name=='sequence'){
			
			if(isNullOrEmpty(object.value)){
			
			alert('请填写顺序！');
			return;
			}else{
			if(strIsNum(object.value)){
				if(object.value>127){
					alert('顺序值只能填写 0~127之间的数字！');
					return;
				}
				}else{
					alert('顺序只能填写 数字！');
					return;
				}
			}
		}
		var typeValue;
		
		if(object.name=='type'){
			typeValue=object.value;
			
		}
		if(object.name=='keywords'){
			value=object.value;
			if(isNullOrEmpty(value) && isNullOrEmpty(typeValue)){
				alert("关键字不可为空，请删除已添加的关键字，方可添加！");
				return;
			}
			//if(typeValue==3 && isNullOrEmpty(value)){
			//alert("当类型为‘可搜索内容’是关键字不能为空！");
			//return;
			//}
		}
		/*
		if(object.name=='value'){
			value=object.value;
			if(!isNullOrEmpty(value)){
				alert(value);
				 var reg=/^[0-9]*@@@[[\u4e00-\u9fa5][0-9][a-z][A-Z].*;$|\\s|/;
				alert(reg.test(value));
				 if(!reg.test(value)){
				 	
				 }else{
				 	alert("请按照合法格式填写（事例：1@@@可用;2@@@不可用;）");
				 	return;
				 }
			}
		}
		*/
	}
	DWREngine.setAsync(false);
	var parameter = getJson(document.getElementById(input));
	eval(helper).insertRecord(namespace + ".save", parameter, {callback:function (returnValue) {
		
		if (returnValue > 0) {
			add(input, list, returnValue, namespace, helper);
			if(helper == "searchDetailHelper"){
				removeOption(tree.item(eval(input),"keywords"));
			}
		} else {
			alert("\u63d2\u5165\u5931\u8d25");
		}
	}});
}

function removeOption(evt) {
	evt = evt ? evt : (window.event.srcElement ? window.event.srcElement : null);
	evt.remove(evt.selectedIndex);
}

function addOption(evt, optionValue) {
	evt = evt ? evt : (window.event.srcElement ? window.event.srcElement : null);
	//alert(evt.nodeName);
	//alert(optionValue);
	var i,arr = new Array();
	evt.options.add(new Option(optionValue, optionValue));
	for (i = evt.options.length - 1; i >= 0; i--) {
		arr.push(evt.options.item(i).value);
		evt.options.remove(i);
	}
	arr.sort(
	function(arrTrs_item1,arrTrs_item2){
		var value1 = arrTrs_item1.slice(arrTrs_item1.firstIndexOfNum(),arrTrs_item1.length);
		var value2 = arrTrs_item2.slice(arrTrs_item2.firstIndexOfNum(),arrTrs_item2.length);
		return value1 - value2;
	});
	for(i=0;i < arr.length; i++){
		evt.options.add(new Option(arr[i], arr[i]))
	}
}

function builderTreeTag(view) {
	DWREngine.setAsync(false);
	var tree;
	catalogHelper.getTree({callback:function (returnValue) {
		tree = document.createElement("ul");
		tree.className = "treeview-popup";
		builderTreeNodesTag(view,returnValue,tree);
		//alert(tree.outerHTML);
	}});
	return tree.outerHTML;
}

function builderTreeNodesTag(view, list, parentTag) {
	var liNode,ulNode = parentTag.nodeName == "UL"?parentTag:document.createElement("ul");
	if(parentTag.nodeName != "UL")
		parentTag.appendChild(ulNode);
	for (var i = 0; i < list.length; i++) {
		if(list[i].status == 0){
			continue;
		}
		liNode = document.createElement("li");
		ulNode.appendChild(liNode);
		liNode.innerHTML = view == 1?"<a href='javascript:void(0);' id='" + list[i].catalogCode + "'>" + list[i].name + "</a>":list[i].layer > 1?"<a href='javascript:void(0);' id='" + list[i].catalogCode + "'>" + list[i].name + "</a>":"<span>" + list[i].name + "</span>";		
		if (!hasChildren(list[i].catalogs))
			continue;
		builderTreeNodesTag(view,list[i].catalogs, liNode);
	}
}

function hasChildren(catalogs){
	var i,len = catalogs.length,flag=false;
	if(catalogs.length == 0){
		return flag;
	}
	for(i=0;i<len;i++){
		if(catalogs[i].status == 1){
			flag = true;
			break;
		}
	}
	return flag;
}

/*
*隐藏显示可搜索内容
*/
function showSearchContent(){
		var main=document.getElementById("searchMain");
		var button=document.getElementById("showButton");
		if(main.style.display=='none'){
			main.style.display='';
			button.value='隐藏可搜索内容';
		}else{
			main.style.display='none';
			button.value='显示可搜索内容';
		}
	}

/*
*去空格
*/
function trim(value){
		return value.replace(/(^\s*)|(\s*$)/g,"");
}
/*
*IN NULL OR EMPTY
*/
function isNullOrEmpty(value){
	if(value != undefined){
		trim(value);
	}
	//value=value.trim();
	var result = false;
	if (value == null || 
		value == undefined || 
		value == "null" || 
		value == ""){
		result = true;
	}
	return result;
}
	
	
// 字符串是否为数字
function strIsNum(str){
	var num;
	
	num = new Number(str);
	return !isNaN(num);
}


/*
*	验证 填写分类
*/
function checkModifyCatalog(){
	var reslut=false;
	var object=document.getElementById('catalog.name');
	if(!isNullOrEmpty(object.value)){
		var	value=document.getElementById("catalog.sequence").value;
		if(!isNullOrEmpty(value)){
			if(strIsNum(value)){
				if(value>127){
					alert('顺序值只能填写 0~127之间的数字！');
					return reslut;
				}else{
					var oEditor = FCKeditorAPI.GetInstance('catalog.htm'); 
					var msg = oEditor.GetXHTML( true );
					var msglength=msg.replace(/[^\x00-\xff]/g,"xx").length;
					if(msglength>508){
						alert('商品简介太长,请精简内容！');
						return reslut;
					}
				}
			}else{
				alert('顺序只能填写 数字！');
				return reslut;
			}
			
		}else{
			alert('请填写顺序！');
			return reslut;
		}
	}else{
		alert('请填写分类名称！');
		return reslut;
	}
	return true;
}
//换SRC
function catalogBody(){
	window.parent.frames("catalogList").location.href=jsCtx+"/jsp/common/body.htm";
}


//删除
function deleteCatalog(selected,Logo){
	dwr.engine.setAsync(false); 
	if(confirm('您确定要删除此分类？')) {
		//var json="{catalogCode:'"+selected+"'}";
		var Parameter = selected;
		//alert(json);
		catalogHelper.getCountByNaeing("catalog_sqlMap.getCatalogByChildCount",Parameter,{callback:function(returnValue){
			if(returnValue>0){
				alert("请先删除子分类！");
				return;
			}else{
				catalogHelper.getCountByNaeing("catalog_sqlMap.getProductByCount",Parameter,{callback:function(returnValue){
					if(returnValue>0){
						alert("请先删除该分类下的商品！");
						return;
					}else{
						window.parent.catalogTree.location.href=jsCtx+'/catalog/catalogMessage.do?method=delteCatalog&catalogCode='+selected+'&isPater='+Logo;
						
					}
				}});
			}
		}});
	}
}
function t(input){
	var TrObject=document.getElementById(input);
	var TdObject = TrObject.children;
		var typeValue;
		var len = TdObject.length;
	for (var i = 0; i < len - 1; i++) {
		object = TdObject.item(i).children.item(0);
		if(object.name=='type'){
			typeValue=object.value;
		}
		if(object.name=='keywords'){
			if(typeValue!=3){
				object.style.display = "none";
				object.disabled = true;
				object.selectedIndex = -1;
			}else{
				object.style.display = "block";
				object.disabled = false;
				object.selectedIndex = -1;
			}
		}
		}
}